package xy.donate.android.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.viewpager.widget.ViewPager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.tabs.TabLayout;
import com.hjq.base.BaseFragmentAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import xy.donate.android.R;
import xy.donate.android.aop.StatusAction;
import xy.donate.android.common.MyActivity;
import xy.donate.android.common.MyFragment;
import xy.donate.android.constant.Constant;
import xy.donate.android.db.HandsUpDB;
import xy.donate.android.db.HandsUpDao;
import xy.donate.android.entity.HandsUpEntity;
import xy.donate.android.helper.MmkvHelper;
import xy.donate.android.ui.fragment.OrderFragment;
import xy.donate.android.widget.StatusLayout;

public class RecordListActivity extends MyActivity implements StatusAction {

    private RecyclerView recyclerView;
    private BaseQuickAdapter<HandsUpEntity, BaseViewHolder> mAdapter;
    private HandsUpDao dao;

    @Override
    protected int getLayoutId() {
        return R.layout.avtivity_record_list;
    }

    @Override
    protected void initView() {

        dao = Room.databaseBuilder(this, HandsUpDB.class, Constant.RECORD_TABLE)
                .allowMainThreadQueries()
                .build().getHandsDao();

        List<HandsUpEntity> list = new ArrayList<>(dao.getList(MmkvHelper.getInstance().getMmkv().decodeString(Constant.USER_ID_NUM)));
        if (list.size() == 0){
            showEmpty();
        }else {
            showComplete();
        }
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new BaseQuickAdapter<HandsUpEntity, BaseViewHolder>(R.layout.item_record_list) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, HandsUpEntity entity) {
                baseViewHolder.setText(R.id.tv_name,entity.getName());
                baseViewHolder.setText(R.id.tv_phone,entity.getPhone());
                baseViewHolder.setText(R.id.tv_order_no,entity.getRecordID());
            }
        };
        mAdapter.addChildClickViewIds(R.id.tv_order_detail);
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            Intent intent = new  Intent(getActivity(), RecordDetailActivity.class);
            intent.putExtra("recordID", mAdapter.getItem(position).getRecordID());
            startActivity(intent);
        });
        recyclerView.setAdapter(mAdapter);

        mAdapter.setList(list);
    }

    @Override
    protected void initData() {

    }

    @Override
    public StatusLayout getStatusLayout() {
        return findViewById(R.id.status_layout);
    }
}